import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'dva'
import { routerRedux } from 'dva/router'
import { Page } from 'components'
import queryString from 'query-string'
import Filter from './components/Filter'
import List from './components/List'
import { ChartCard } from 'components/Charts'
import { Tabs } from 'antd'
const { TabPane } = Tabs
import StatisticalDiagram from './components/StatisticalDiagram'

const StatisticsOfDialogue = ({ location, dispatch, loading, statisticsOfDialogue }) => {
  const { query, pathname } = location
  const { list, pagination, empList, departments, condition, tabsKey, dataOfDiagram, adAcquireType, filter, start, end} = statisticsOfDialogue
  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery
      })
    }))
  }

  const filterProps = {
    filter: {
      ...filter
    },
    adAcquireType,
    condition,
    empList,
    departments,
    tabsKey,
    onFilterChange(value) {
      switch (tabsKey) {
        case "table_form":
          dispatch({
            type: "statisticsOfDialogue/query",
            payload: {
              ...value
            }
          })
          break;
        case "statistical_chart":
          dispatch({
            type: "statisticsOfDialogue/queryStatisticalDiagram",
            payload: {
              ...value
            }
          })
          break;
        case "region_distribution":
          dispatch({
            type: "statisticsOfDialogue/getChinaMapData",
            payload: {
              ...value
            }
          })
          break;
      }

      if (value && value.start) {
        dispatch({
          type: 'statisticsOfDialogue/updateState',
          payload: {
            start: new Date(value.start),
            end: new Date(value.end),
            filter: value,
          },
        })
      }
    },
    handleSearch(key) {
      if (key) {
        dispatch({
          type: 'statisticsOfDialogue/searchEmp',
          payload: {
            key,
          },
        })
      }
    },
    handleOnChange(value) {
      dispatch({
        type: 'statisticsOfDialogue/updateCondition',
        payload: {
          condition: value
        }
      })
    },
  }

  const listProps = {
    start,
    end,
    rowKey: 'id',
    dataSource: list,
    loading: loading.effects['statisticsOfDialogue/query'],
    pagination,
    location,
    onChange(page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize
      })
    }
  }

  const handleTabsKeyOnChange = (value) => {
    dispatch({
      type: 'statisticsOfDialogue/updateTabsKey',
      payload: {
        tabsKey: value
      }
    })
  }

  return (
    <div>
      <ChartCard style={{ marginBottom: 12 }}>
        <Filter {...filterProps} />
      </ChartCard>
      <Page inner>
        <Tabs defaultActiveKey='date_table' onChange={handleTabsKeyOnChange} >
          <TabPane tab="统计表格" key="table_form">
            <List {...listProps} />
          </TabPane>
          {/*<TabPane tab="统计图表" key="statistical_diagram">
            <StatisticalDiagram list={list} />
          </TabPane>*/}
        </Tabs>
      </Page>
    </div>
  )
}

StatisticsOfDialogue.propTypes = {
  statisticsOfDialogue: PropTypes.object,
  location: PropTypes.object,
  loading: PropTypes.object
}
export default connect(({ statisticsOfDialogue, loading }) => ({ statisticsOfDialogue, loading }))(StatisticsOfDialogue)
